local nya__juxiang = fk.CreateSkill {

  name = "nya__juxiang",

  tags = { Skill.Compulsory, },

}



nya__juxiang:addEffect(fk.PreCardEffect, {
  name = "nya__juxiang",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.CardUseFinished, fk.AfterCardsMove, fk.AfterCardUseDeclared, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(nya__juxiang.name) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).trueName == "savage_assault" and player.room:getCardArea(info.cardId) == Card.DiscardPile then
                return true
              end
            end
          end
        end
      elseif data.card and data.card.trueName == "savage_assault" then
        if event == fk.PreCardEffect then
          return data.to == player.id
        elseif event == fk.CardUseFinished then
          return target ~= player and player.room:getCardArea(data.card) == Card.Processing
        elseif event == fk.AfterCardUseDeclared then
          return target == player
        elseif event == fk.DamageCaused then
          return target == player and not data.to:hasSkill("nya__play", true)
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(nya__juxiang.name)
    if event == fk.PreCardEffect then
      room:notifySkillInvoked(player, nya__juxiang.name, "defensive")
      return true
    elseif event == fk.AfterCardsMove then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == "savage_assault" and room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      if #ids > 0 then
        room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(ids)
        room:obtainCard(player, dummy, true, fk.ReasonJustMove)
      end
    elseif event == fk.CardUseFinished then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      room:obtainCard(player, data.card, true, fk.ReasonJustMove)
    elseif event == fk.AfterCardUseDeclared then
      room:notifySkillInvoked(player, nya__juxiang.name, "offensive")
      data.disresponsiveList = data.disresponsiveList or {}
      for _, p in ipairs(room.alive_players) do
        if p:hasSkill("nya__play", true) then
          table.insertIfNeed(data.disresponsiveList, p.id)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      player:drawCards(1, nya__juxiang.name)
    end
  end,
})
nya__juxiang:addEffect(fk.CardUseFinished, {
  name = "nya__juxiang",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.CardUseFinished, fk.AfterCardsMove, fk.AfterCardUseDeclared, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(nya__juxiang.name) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).trueName == "savage_assault" and player.room:getCardArea(info.cardId) == Card.DiscardPile then
                return true
              end
            end
          end
        end
      elseif data.card and data.card.trueName == "savage_assault" then
        if event == fk.PreCardEffect then
          return data.to == player.id
        elseif event == fk.CardUseFinished then
          return target ~= player and player.room:getCardArea(data.card) == Card.Processing
        elseif event == fk.AfterCardUseDeclared then
          return target == player
        elseif event == fk.DamageCaused then
          return target == player and not data.to:hasSkill("nya__play", true)
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(nya__juxiang.name)
    if event == fk.PreCardEffect then
      room:notifySkillInvoked(player, nya__juxiang.name, "defensive")
      return true
    elseif event == fk.AfterCardsMove then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == "savage_assault" and room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      if #ids > 0 then
        room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(ids)
        room:obtainCard(player, dummy, true, fk.ReasonJustMove)
      end
    elseif event == fk.CardUseFinished then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      room:obtainCard(player, data.card, true, fk.ReasonJustMove)
    elseif event == fk.AfterCardUseDeclared then
      room:notifySkillInvoked(player, nya__juxiang.name, "offensive")
      data.disresponsiveList = data.disresponsiveList or {}
      for _, p in ipairs(room.alive_players) do
        if p:hasSkill("nya__play", true) then
          table.insertIfNeed(data.disresponsiveList, p.id)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      player:drawCards(1, nya__juxiang.name)
    end
  end,
})
nya__juxiang:addEffect(fk.AfterCardsMove, {
  name = "nya__juxiang",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.CardUseFinished, fk.AfterCardsMove, fk.AfterCardUseDeclared, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(nya__juxiang.name) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).trueName == "savage_assault" and player.room:getCardArea(info.cardId) == Card.DiscardPile then
                return true
              end
            end
          end
        end
      elseif data.card and data.card.trueName == "savage_assault" then
        if event == fk.PreCardEffect then
          return data.to == player.id
        elseif event == fk.CardUseFinished then
          return target ~= player and player.room:getCardArea(data.card) == Card.Processing
        elseif event == fk.AfterCardUseDeclared then
          return target == player
        elseif event == fk.DamageCaused then
          return target == player and not data.to:hasSkill("nya__play", true)
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(nya__juxiang.name)
    if event == fk.PreCardEffect then
      room:notifySkillInvoked(player, nya__juxiang.name, "defensive")
      return true
    elseif event == fk.AfterCardsMove then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == "savage_assault" and room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      if #ids > 0 then
        room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(ids)
        room:obtainCard(player, dummy, true, fk.ReasonJustMove)
      end
    elseif event == fk.CardUseFinished then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      room:obtainCard(player, data.card, true, fk.ReasonJustMove)
    elseif event == fk.AfterCardUseDeclared then
      room:notifySkillInvoked(player, nya__juxiang.name, "offensive")
      data.disresponsiveList = data.disresponsiveList or {}
      for _, p in ipairs(room.alive_players) do
        if p:hasSkill("nya__play", true) then
          table.insertIfNeed(data.disresponsiveList, p.id)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      player:drawCards(1, nya__juxiang.name)
    end
  end,
})
nya__juxiang:addEffect(fk.AfterCardUseDeclared, {
  name = "nya__juxiang",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.CardUseFinished, fk.AfterCardsMove, fk.AfterCardUseDeclared, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(nya__juxiang.name) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).trueName == "savage_assault" and player.room:getCardArea(info.cardId) == Card.DiscardPile then
                return true
              end
            end
          end
        end
      elseif data.card and data.card.trueName == "savage_assault" then
        if event == fk.PreCardEffect then
          return data.to == player.id
        elseif event == fk.CardUseFinished then
          return target ~= player and player.room:getCardArea(data.card) == Card.Processing
        elseif event == fk.AfterCardUseDeclared then
          return target == player
        elseif event == fk.DamageCaused then
          return target == player and not data.to:hasSkill("nya__play", true)
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(nya__juxiang.name)
    if event == fk.PreCardEffect then
      room:notifySkillInvoked(player, nya__juxiang.name, "defensive")
      return true
    elseif event == fk.AfterCardsMove then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == "savage_assault" and room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      if #ids > 0 then
        room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(ids)
        room:obtainCard(player, dummy, true, fk.ReasonJustMove)
      end
    elseif event == fk.CardUseFinished then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      room:obtainCard(player, data.card, true, fk.ReasonJustMove)
    elseif event == fk.AfterCardUseDeclared then
      room:notifySkillInvoked(player, nya__juxiang.name, "offensive")
      data.disresponsiveList = data.disresponsiveList or {}
      for _, p in ipairs(room.alive_players) do
        if p:hasSkill("nya__play", true) then
          table.insertIfNeed(data.disresponsiveList, p.id)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      player:drawCards(1, nya__juxiang.name)
    end
  end,
})
nya__juxiang:addEffect(fk.DamageCaused, {
  name = "nya__juxiang",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.CardUseFinished, fk.AfterCardsMove, fk.AfterCardUseDeclared, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(nya__juxiang.name) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).trueName == "savage_assault" and player.room:getCardArea(info.cardId) == Card.DiscardPile then
                return true
              end
            end
          end
        end
      elseif data.card and data.card.trueName == "savage_assault" then
        if event == fk.PreCardEffect then
          return data.to == player.id
        elseif event == fk.CardUseFinished then
          return target ~= player and player.room:getCardArea(data.card) == Card.Processing
        elseif event == fk.AfterCardUseDeclared then
          return target == player
        elseif event == fk.DamageCaused then
          return target == player and not data.to:hasSkill("nya__play", true)
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(nya__juxiang.name)
    if event == fk.PreCardEffect then
      room:notifySkillInvoked(player, nya__juxiang.name, "defensive")
      return true
    elseif event == fk.AfterCardsMove then
      local ids = {}
      for _, move in ipairs(data) do
        if move.from ~= player.id and move.moveReason == fk.ReasonDiscard then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == "savage_assault" and room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        end
      end
      if #ids > 0 then
        room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
        local dummy = Fk:cloneCard("dilu")
        dummy:addSubcards(ids)
        room:obtainCard(player, dummy, true, fk.ReasonJustMove)
      end
    elseif event == fk.CardUseFinished then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      room:obtainCard(player, data.card, true, fk.ReasonJustMove)
    elseif event == fk.AfterCardUseDeclared then
      room:notifySkillInvoked(player, nya__juxiang.name, "offensive")
      data.disresponsiveList = data.disresponsiveList or {}
      for _, p in ipairs(room.alive_players) do
        if p:hasSkill("nya__play", true) then
          table.insertIfNeed(data.disresponsiveList, p.id)
        end
      end
    elseif event == fk.DamageCaused then
      room:notifySkillInvoked(player, nya__juxiang.name, "drawcard")
      player:drawCards(1, nya__juxiang.name)
    end
  end,
})

return nya__juxiang